package nativejpa.test.jpa.derby;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;
import javax.persistence.SqlResultSetMappings;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.NamedQuery;
import javax.persistence.ColumnResult;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import java.io.Serializable;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 * @create 2009-3-20 16:28:01
 */
@Entity
@Table(name = "COUNTRIES")
@NamedNativeQueries({
        @NamedNativeQuery(name = Country.GET_ALL_COUNTRIES, // error sql for test dialect
                query = "select from COUNTRIES",
                hints = {
                        @QueryHint(name = "query.dialect.derby", value="select * from COUNTRIES")
                }
        ),
        @NamedNativeQuery(name = Country.GET_COUNTS, // test sqlResultsetMapping.columns
                query = "select count(*) as country_count from COUNTRIES",
                resultSetMapping = Country.GET_COUNTS
        ),
        @NamedNativeQuery(name=Country.GET_ALL_COUNTRIES_SQL_MAPPING, // test sqlResultsetMaping.entities
                query = "select COUNTRY_ISO_CODE as id, COUNTRY, REGION from COUNTRIES",
                resultSetMapping = Country.GET_ALL_COUNTRIES_SQL_MAPPING
        ),
        @NamedNativeQuery(name=Country.GET_ALL_COUNTRIES_MAPPING_FILE, // bad query, will overrite by external Mapping file
                query = "select * from COUNTRY",
                resultClass = City.class
        )

})
@SqlResultSetMappings(
        {
                @SqlResultSetMapping(
                        name = Country.GET_COUNTS,
                        columns = {@ColumnResult(name = "country_count")}
                ),
                @SqlResultSetMapping(
                        name = Country.GET_ALL_COUNTRIES_SQL_MAPPING,
                        entities = {@EntityResult(entityClass = Country.class, fields = {@FieldResult(name="COUNTRY_ISO_CODE", column = "id")})}
                )
        }
)
@NamedQuery(name = "testNamedQuery", query = "select c from Country c") // just a NamedQuery for test warning
public class Country implements Serializable {

    public static final String GET_ALL_COUNTRIES = "getAllCountries";
    public static final String GET_ALL_COUNTRIES_SQL_MAPPING = "getAllCountries_sqlmapping";
    public static final String GET_COUNTS = "getCount";
    public static final String GET_ALL_COUNTRIES_MAPPING_FILE = "GET_ALL_COUNTRIES_MAPPING_FILE";

    @Id
    @Column(name = "COUNTRY_ISO_CODE")
    private String countryCode;

    @Column(name = "COUNTRY")
    private String country;

    @Column(name = "REGION")
    private String region;

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "{NAME=" + getCountry() + ", CODE=" + getCountryCode() + "}";
    }
}
